<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="riskPoint">
        <el-input v-model="queryParams.riskPoint" placeholder="请输入风险点" clearable size="middle" @keyup.enter.native="handleQuery"/>
      </el-form-item>
     <el-form-item label="" prop="riskType">
        <el-select v-model="queryParams.riskType" placeholder="请选择风险类型" clearable size="middle" @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 风险列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="风险点" align="center" prop="riskPoint" />
      <el-table-column label="类型" align="center" prop="riskType">
        <template slot-scope="scope" >
          <span v-if="scope.row.riskType == 1">基础管理</span>
          <span v-if="scope.row.riskType == 2">作业活动</span>
          <span v-if="scope.row.riskType == 3">设备设施</span>
        </template>
      </el-table-column>
      <el-table-column label="可能发生的事故类型" align="center" prop="accidentTypeId">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.ACCIDENT" :value="scope.row.accidentTypeId" />
        </template>
      </el-table-column>
      <el-table-column label="区域位置" align="center" prop="regionSite" />
      <el-table-column label="所属单位" align="center" prop="companyName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" >
          <el-button size="mini" type="text" icon="el-icon-edit" @click="openHazardResource(scope.row)"
                     v-hasPermi="['biz:risk-info:update']">危险源</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <el-dialog :title="hazardTitle" :visible.sync="hazardOpen" width="60%" append-to-body>
      <HazardResource :riskId="riskId" :isEdit="isEdit"></HazardResource>
    </el-dialog>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="所属单位" prop="companyName">
          <el-input v-model="form.companyName" readonly/>
        </el-form-item>
        <el-form-item label="风险点名称" prop="riskPoint">
          <el-input v-model="form.riskPoint" placeholder="请输入风险点名称" />
        </el-form-item>
        <el-form-item label="可能发生的事故类型" prop="accidentTypeId">
          <el-select style="width: 100%;" v-model="form.accidentTypeId" placeholder="请选择事故类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.ACCIDENT)"
                      :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="区域位置" prop="regionSite">
          <el-input v-model="form.regionSite" placeholder="请输入区域位置" />
        </el-form-item>
        <el-form-item label="责任单位" prop="responsibleId">
            <treeselect v-model="form.responsibleId" :options="checkDeptOptions" :show-count="true"
                    placeholder="请选择责任单位" :normalizer="normalizer"/>
        </el-form-item>
        <el-form-item label="责任人" prop="directPersonId">
          <el-select style="width: 100%;" v-model="form.directPersonId" placeholder="请选择责任人" >
            <el-option v-for="item in users" :key="parseInt(item.id)" :label="item.nickname" :value="parseInt(item.id)" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remarks" placeholder="请输入备注" type="textarea"/>
        </el-form-item>
     </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRiskInfoPage, exportRiskInfoExcel,getRiskInfo,updateRiskInfo} from "@/api/biz/riskInfo";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import HazardResource from "@/views/biz/hazardResource";
import { getDeptByCurrentUser } from "@/api/system/dept";
import { listSimpleDepts } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import { listSimpleUsers } from "@/api/system/user";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "RiskInfo",
  components: {
    HazardResource,
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 风险数据列表
      list: [],
      //危险源弹出层标题
      hazardTitle: "",
      // 是否显示危险源弹出层
      hazardOpen: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        riskPoint: null,
        riskType: null,
      },
       //风险类型下拉框
      options: [{
          value: '1',
          label: '基础管理'
        }, {
          value: '2',
          label: '作业活动'
        }, {
          value: '3',
          label: '设备设施'
        }],
      riskId:'',
      isEdit:true,
      // 表单参数
      form: {
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        companyName:[
          { required: true, message: "请输入所属单位", trigger: "blur" }
        ],
        riskPoint: [
          { required: true, message: "请输入风险点", trigger: "blur" }
        ],
        regionSite: [
          { required: true, message: "请输入区域场所", trigger: "blur" }
        ],
        hazardSource: [
          { required: true, message: "请输入危险源", trigger: "blur" }
        ]
      },
       //检查部门
      checkDeptOptions:undefined,
      // 用户下拉列表
      users: [],
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    // 获得用户列表
    listSimpleUsers().then(response => {
      this.users = response.data;
    });
  },
  mounted(){
    this.getList();
  },
  methods: {
    /** 通过当前登录人员获取所属单位 */
    getDeptByCurrentUser(){
      getDeptByCurrentUser().then(response=>{
        this.form.companyName = response.data.name
        this.form.companyId = response.data.id
      });
    },
    /** 查询部门下拉树结构 + 人员下拉 */
    getTreeselect() {
      listSimpleDepts().then(response => {
        //检查部门
        this.checkDeptOptions = [];
        this.checkDeptOptions.push(...this.handleTree(response.data, "id"));
        //被查部门
        this.checkbyDeptOptions = [];
        this.checkbyDeptOptions.push(...this.handleTree(response.data, "id"));
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      // 执行查询
      getRiskInfoPage(params).then(response => {
        this.list = response.list;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    openHazardResource(row){
      //打开危险源
      this.riskId = row.id;
      this.hazardTitle = '危险源';
      this.hazardOpen = true;
    },
     /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getRiskInfo(id).then(response => {
        console.log(response)
        this.form = response.data;
        this.open = true;
        this.title = "修改风险数据";
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateRiskInfo(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
      });
    },
    /** 表单重置 */
    reset() {
      this.form = {
        riskPoint: undefined,
        companyName: undefined,
        companyId: undefined,
        regionSite: undefined,
        accidentTypeId: undefined,
        remarks: undefined,
        riskTypeId: undefined,
        responsibleId:undefined,
      };
      this.resetForm("form");
    },
    // 格式化部门的下拉框
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    }
  }
};
</script>
